package com.cnunicom.modules.addressbook.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.cnunicom.modules.addressbook.entity.ChinaEntity;
import com.cnunicom.modules.addressbook.service.ChinaService;
import com.cnunicom.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cnunicom.modules.addressbook.entity.AddressBookEntity;
import com.cnunicom.modules.addressbook.service.AddressBookService;
import com.cnunicom.common.utils.PageUtils;
import com.cnunicom.common.utils.R;

import javax.annotation.Resource;


/**
 * 通讯录表
 *
 * @author dingziyuan
 * @email dzy998877@163.com
 * @date 2019-07-11 10:34:23
 */
@RestController
@RequestMapping("addressbook/addressbook")
public class AddressBookController extends AbstractController {
    @Autowired
    private AddressBookService addressBookService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("addressbook:addressbook:list")
    public R list(@RequestParam Map<String, Object> params) {
        params.put("user", getUser());
        PageUtils page = addressBookService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("addressbook:addressbook:info")
    public R info(@PathVariable("id") Long id) {
        AddressBookEntity addressBook = addressBookService.getById(id);

        return R.ok().put("addressBook", addressBook);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("addressbook:addressbook:save")
    public R save(@RequestBody AddressBookEntity addressBook) {
        addressBook.setCreateTime(new Date());
        addressBookService.save(addressBook);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("addressbook:addressbook:update")
    public R update(@RequestBody AddressBookEntity addressBook) {
        addressBookService.updateById(addressBook);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("addressbook:addressbook:delete")
    public R delete(@RequestBody Long[] ids) {
        addressBookService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
